package data_structure;

public class leetcode_0036 {
    public boolean isValidSudoku(char[][] board) {
        for(int i=0;i<9;++i){
            boolean[] flag1=new boolean[9];
            boolean[] flag2=new boolean[9];
            zero(flag1);zero(flag2);
            for(int j=0;j<9;++j){
                if(board[i][j]-'0'>0&&board[i][j]-'0'<10){
                    if(flag1[board[i][j]-'0'-1]){
                        return false;
                    }
                    else{
                        flag1[board[i][j]-'0'-1]=true;
                    }
                }
                if(board[j][i]-'0'>0&&board[j][i]-'0'<10){
                    if(flag2[board[j][i]-'0'-1]){
                        return false;
                    }
                    else{
                        flag2[board[j][i]-'0'-1]=true;
                    }
                }
            }
        }

        for(int i=0;i<3;++i){
            for(int j=0;j<3;++j){
                boolean[] flag1=new boolean[9];
                zero(flag1);
                for(int k=0;k<3;++k){
                    for(int l=0;l<3;++l){
                        if(board[i*3+k][j*3+l]-'0'>0&&board[i*3+k][j*3+l]-'0'<10){
                            if(flag1[board[i*3+k][j*3+l]-'0'-1]){
                                return false;
                            }
                            else{
                                flag1[board[i*3+k][j*3+l]-'0'-1]=true;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
    public void zero(boolean[] flag){
        for(int i=0;i<9;++i){
            flag[i]=false;
        }
    }
}
